package com.zhs.gccs.dao;

import com.zhs.gccs.model.CaFile;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.List;

//任务轨迹
public class TaskGccjFileDao {
    private final DbManager dbManager;

    public TaskGccjFileDao() {
        this.dbManager = XUtilsManager.getInstance().getDbManager();
    }

    public void addOrUpdate(CaFile caFile) {
        try {
//            CaFile caDictTmp = findLocalId(caFile.getLocalId());
//            if(caDictTmp!=null){
//                caFile.setId(caDictTmp.getId());
//            }
            dbManager.saveOrUpdate(caFile);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public CaFile findLocalId(int id) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("localId", "=", id);
            CaFile first = dbManager
                    .selector(CaFile.class).where(whereBuilder).findFirst();
            return first;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }
    //
    public CaFile findByFileId(String fileId) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("fileId", "=", fileId);
            CaFile first = dbManager
                    .selector(CaFile.class).where(whereBuilder).findFirst();
            return first;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<CaFile> findByDataId(String dataId) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("dataId", "=", dataId);
            List<CaFile> first = dbManager
                    .selector(CaFile.class).where(whereBuilder)
                    .findAll();
            return first;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<CaFile> findByDataIdAndType(String dataId, String type_image) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.
                    b("dataId", "=", dataId).
                    and("fileType","=",type_image);
            List<CaFile> first = dbManager
                    .selector(CaFile.class).where(whereBuilder)
                    .findAll();
            return first;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void clearDB(){
        List<CaFile> list = null;
        try {
            list = dbManager.selector(CaFile.class).findAll();
            if(null!=list && list.size()>0){
                dbManager.delete(list);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<CaFile> findNeedSyncListByTaskId(String taskId) {
        List<CaFile> list = null;
        try {
            WhereBuilder whereBuilder = WhereBuilder
                    .b("taskId", "=", taskId)
                    .and("isSync","=",false);
            list = dbManager.selector(CaFile.class).where(whereBuilder).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void deleteByDataId(String dataId) {
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("dataId", "=", dataId);
            dbManager.delete(CaFile.class,whereBuilder);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }



}
